package com.weigee.ktv.v.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.l.d.app.library.eventbus.EventSendEntity;
import com.l.d.app.library.util.ToastUtil;
import com.l.d.app.library.view.noscroll.NoScrollGridView;
import com.weigee.ktv.R;
import com.weigee.ktv.m.EventTag;
import com.weigee.ktv.m.retrofit.ApiRequest;
import com.weigee.ktv.v.adapter.grid.RechargeReturnAdapter;
import com.weigee.ktv.v.view.WrapTitleBar;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;
import skin.support.widget.SkinCompatEditText;
import skin.support.widget.SkinCompatFrameLayout;
import skin.support.widget.SkinCompatTextView;

/**
 * Created by linde on 2017/04/24.
 * <p/>
 * 充值返利
 */
public class RechargeReturnFragment extends BaseFragment implements TextWatcher {
    @BindView(R.id.wrap_title_bar)
    WrapTitleBar mTitleBar;
    @BindView(R.id.tv_balance)
    SkinCompatTextView m_tvBalance;
    @BindView(R.id.grid_view)
    NoScrollGridView m_gv;

    @BindView(R.id.frameCommit)
    SkinCompatFrameLayout m_groupCommit;
    @BindView(R.id.tv_commit)
    SkinCompatTextView m_tvCommit;

    @BindView(R.id.et_recharge)
    SkinCompatEditText m_etRecharge;

    private RechargeReturnAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.rechargereturn_frag, container, false);
    }

    @Override
    protected void initView(View view, @Nullable Bundle savedInstanceState) {
        mTitleBar.setTitle("充值返利");
        mTitleBar.setLeftButton(View.VISIBLE, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        ApiRequest.balance(m_tvBalance);

        mAdapter = new RechargeReturnAdapter();
        m_gv.setAdapter(mAdapter);

        ApiRequest.rechargeReturn(mAdapter, false, JSONObject.class);
        setCommitBtnEnabled(false, 0);

        m_etRecharge.addTextChangedListener(this);
    }

    private void setCommitBtnEnabled(boolean enabled, int amountWillReturn) {
        m_groupCommit.setEnabled(enabled);
        m_tvCommit.setEnabled(enabled);
        if (enabled && amountWillReturn > 0) {
            m_tvCommit.setText(String.valueOf("立即充值(到账" + amountWillReturn + ")"));
        } else {
            m_tvCommit.setText(String.valueOf("立即充值"));
        }
    }

    @OnClick(R.id.tv_commit)
    protected void onCommitClick(View view) {
        // TODO: 2017/04/25 立即充值
        ToastUtil.makeTest(view.getContext(), "recharge");
    }

    // 选中
    @Subscriber(tag = EventTag.EVENT_RECHARGE_RETURN_CHECK_AMOUNT_RETURN)
    private void onRechargeCheckedUpdate(EventSendEntity send) {
        if (mAdapter == null) {
            return;
        }
        final int amount = mAdapter.getSelectedAmount();
        if (amount <= 0) {
            setCommitBtnEnabled(false, 0);
        } else {
            setCommitBtnEnabled(true, amount);
        }
        setEditTextAmountWillRecharge("");
    }

    private void setEditTextAmountWillRecharge(String text) {
        m_etRecharge.removeTextChangedListener(this);
        m_etRecharge.setText(text);
        m_etRecharge.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (TextUtils.isEmpty(s)) {
            return;
        }
        final Context context = m_etRecharge.getContext();
        final String str = String.valueOf(s);
        int amountWillRecharge = 0;
        try {
            amountWillRecharge = Integer.parseInt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mAdapter != null) {
            mAdapter.clearAmountChecked();
        }
        if (amountWillRecharge <= 0) {
            setEditTextAmountWillRecharge("");
            ToastUtil.makeTest(context, "充值金额不能少于0");
            setCommitBtnEnabled(false, 0);
        } else {
            int iReturn = 0;
            if (mAdapter != null) {
                iReturn = mAdapter.getAmountReturnIfRecharge(amountWillRecharge);
            }
            setCommitBtnEnabled(true, iReturn);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
}
